<?php
session_start();

//création de la courbe de poids


include ("../../jpGraph/src/jpgraph.php");
include ("../../jpGraph/src/jpgraph_line.php");

require_once dirname(__FILE__) . "/../../includes/config.php";
require_once PHP_ROOTPATH.'/classes/database.php';

$id_anim = (isset($_GET["id_anim"])) ? html_entity_decode($_GET["id_anim"]) : NULL;

if($id_anim){
	$idconnect=Database::getInstance();
										 
	$query = pg_query($idconnect,"SELECT mesure,date_test FROM resultat_test_simple,resultat_test,test,type_test 
	WHERE id_animal='".$id_anim."'
	AND resultat_test.id_test=test.id_test
	AND resultat_test.id_res=resultat_test_simple.id_res
	AND test.id_type_test=type_test.id_type_test
	AND type_test.label LIKE '%Pesée%' ORDER BY date_test ASC ");
							
	$ydata=array();
	$xdata=array();
	while ($back = pg_fetch_assoc($query)) {
		if(preg_match('/^(\d+).*$/',$back["mesure"],$tab)){	array_push($ydata,$tab[1]);}
		else{array_push($ydata,$back["mesure"]);}
		array_push($xdata,date("d/m/Y", strtotime($back["date_test"])));
	}	
	$ydata2 = array(8,3,16,2,7,25,16);

	// Creation du graphique
	$graph = new Graph(500,300);
	$graph->img->SetMargin(60,20,35,75);
	$graph->SetScale("textlin");
	$graph->SetMarginColor("white");

	// Création du système de points
	$lineplot=new LinePlot($ydata);

	// On rajoute les points au graphique
	$graph->Add($lineplot);
	
	// Ajouter le titre du graphique
	$graph->title->Set("Evolution du poids");
	// Paramétrage des axes

	$graph->title->SetMargin(8);
	$graph->title->SetFont(FF_VERDANA,FS_BOLD,12);
	$graph->title->SetColor("blue:0.5");

	// Setup font for axis
	$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,8);
	$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,8);
	 
	// Show 0 label on Y-axis (default is not to show)
	$graph->yscale->ticks->SupressZeroLabel(false);
	 
	// Setup X-axis labels
	$graph->xaxis->SetTickLabels($xdata);
	$graph->xaxis->SetLabelAngle(50);
	
	$graph->xaxis->title->Set("Date");
	$graph->yaxis->title->Set("Poids");
	// Affichage
	header("Content-type: image/png");
	$graph->Stroke();
}
?>
